package com.tanhua.dubbo.mongo.api;


import com.tanhua.model.mongo.Comment;
import com.tanhua.model.mongo.Movement;
import com.tanhua.model.mongo.Video;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.FindAndModifyOptions;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

@DubboService
public class CommentApiImpl implements CommentApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    //分页查询评论列表
    @Override
    public List<Comment> findAllComments(String movementId, Integer page, Integer pagesize, int commentType) {

        Criteria criteria = Criteria.where("publishId").is(new ObjectId(movementId))
                .and("commentType").is(commentType);
        Query query = Query.query(criteria).with(Sort.by(Sort.Order.desc("created")))
                .skip((page - 1) * pagesize).limit(pagesize);

        List<Comment> comments = mongoTemplate.find(query, Comment.class);

        return comments;
    }

    //评论,点赞,喜欢-提交
    public Integer saveComment(Comment comment) {

        ObjectId movementId = comment.getPublishId();

        //根据动态id获得动态数据
        Movement movement = mongoTemplate.findById(movementId, Movement.class);
        //存入被评论人id
        comment.setPublishUserId(movement.getUserId());

        //将评论数据保存到评论表中
        mongoTemplate.save(comment);

        // 修改动态表中的评论总数,并返回最新值,

        //参数一 :Query ,构建条件
        Query query = Query.query(Criteria.where("id").is(movementId));
        //参数二 : Update对象 ,更新字段,更新赋值
        Update update = new Update();
        //判断
        //对此字段 +1
        if (comment.getCommentType()==1){
            //点赞
            update.inc("likeCount",1);
        }else if (comment.getCommentType()==2){
            //评论
            update.inc("commentCount",1);
        }else {
            //喜欢
            update.inc("loveCount",1);
        }


        //是否返回最新数值对象 true 为返回
        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(true);

        //先更新在返回更新结果-->findAndModify
        Movement andModify = mongoTemplate.findAndModify(query, update, options, Movement.class);

        //判断 //通过get方法 获得该动态对应的总评论数
        if (comment.getCommentType()==1){
            //点赞
            return andModify.getLikeCount();
        }else if (comment.getCommentType()==2){
            //评论
            return andModify.getCommentCount();
        }else {
            //喜欢
            return andModify.getLoveCount();
        }

    }

    //评论,点赞,喜欢-提交
    public Integer saveVideoComment(Comment comment) {

        ObjectId movementId = comment.getPublishId();

        //根据动态id获得动态数据
        Video video = mongoTemplate.findById(movementId, Video.class);
        //存入被评论人id
        comment.setPublishUserId(video.getUserId());

        //将评论数据保存到评论表中
        mongoTemplate.save(comment);

        // 修改动态表中的评论总数,并返回最新值,

        //参数一 :Query ,构建条件
        Query query = Query.query(Criteria.where("id").is(movementId));
        //参数二 : Update对象 ,更新字段,更新赋值
        Update update = new Update();
        //判断
        //对此字段 +1
        if (comment.getCommentType()==1){
            //点赞
            update.inc("likeCount",1);
        }else if (comment.getCommentType()==2){
            //评论
            update.inc("commentCount",1);
        }else {
            //喜欢
            update.inc("loveCount",1);
        }


        //是否返回最新数值对象 true 为返回
        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(true);

        //先更新在返回更新结果-->findAndModify
        Video andModify = mongoTemplate.findAndModify(query, update, options, Video.class);

        //判断 //通过get方法 获得该动态对应的总评论数
        if (comment.getCommentType()==1){
            //点赞
            return andModify.getLikeCount();
        }else if (comment.getCommentType()==2){
            //评论
            return andModify.getCommentCount();
        }else {
            //喜欢
            return andModify.getLoveCount();
        }

    }
    //查询该条动态"我"是否有点赞,返回值布尔
    public Boolean hasComment(String movementId, Long userId, int commentType) {

        Criteria criteria = Criteria.where("publishId").is(new ObjectId(movementId))
                  .and("userId").is(userId).and("commentType").is(commentType);
        Query query = Query.query(criteria);

        boolean exists = mongoTemplate.exists(query, Comment.class);
        return exists;
    }

    //动态-取消点赞 并返回动态的总点赞数 ////动态-取消喜欢 并返回动态的总喜欢数
    public Integer dislike(String movementId, Long userId, int commentType) {

        //删除Comment表数据
        Criteria criteria = Criteria.where("publishId").is(new ObjectId(movementId))
                .and("userId").is(userId).and("commentType").is(commentType);
        Query query = Query.query(criteria);
        mongoTemplate.remove(query,Comment.class);

        //更新并返回总数

        Query query1 = Query.query(Criteria.where("id").is(new ObjectId(movementId)));


        Update update=new Update();

        //总数-1
        if (commentType==1){
            //点赞
            update.inc("likeCount",-1);
        }else if (commentType==2){
            //评论
            update.inc("commentCount",-1);
        }else {
            //喜欢
            update.inc("loveCount",-1);
        }

        FindAndModifyOptions options=new FindAndModifyOptions();
        options.returnNew(true);

        Movement modify = mongoTemplate.findAndModify(query1, update, options, Movement.class);

        //返回
        if (commentType==1){
            //点赞
            return modify.getLikeCount();
        }else if (commentType==2){
            //评论
            return modify.getCommentCount();
        }else {
            //喜欢
            return modify.getLoveCount();
        }
    }


    //小视频-取消点赞 并返回动态的总点赞数 ////动态-取消喜欢 并返回动态的总喜欢数
    public Integer dislikeVideo(String videoId, Long userId, int commentType) {

        //删除Comment表数据
        Criteria criteria = Criteria.where("publishId").is(new ObjectId(videoId))
                .and("userId").is(userId).and("commentType").is(commentType);
        Query query = Query.query(criteria);
        mongoTemplate.remove(query,Comment.class);

        //更新并返回总数

        Query query1 = Query.query(Criteria.where("id").is(new ObjectId(videoId)));


        Update update=new Update();

        //总数-1
        if (commentType==1){
            //点赞
            update.inc("likeCount",-1);
        }else if (commentType==2){
            //评论
            update.inc("commentCount",-1);
        }else {
            //喜欢
            update.inc("loveCount",-1);
        }

        FindAndModifyOptions options=new FindAndModifyOptions();
        options.returnNew(true);

        Video modify = mongoTemplate.findAndModify(query1, update, options, Video.class);

        //返回
        if (commentType==1){
            //点赞
            return modify.getLikeCount();
        }else if (commentType==2){
            //评论
            return modify.getCommentCount();
        }else {
            //喜欢
            return modify.getLoveCount();
        }
    }

    //根据动态id查询到动态数据
    public Comment findComments(Object commentsId) {
        Criteria criteria = Criteria.where("id").is(commentsId);
        Query query = Query.query(criteria);
        return mongoTemplate.findOne(query,Comment.class);
    }

    //小视频的评论点赞
    public void addCommentsLike(Comment comment,Object commentId) {
        Query query = Query.query(Criteria.where("id").is(commentId));
        Update update = new Update();
        update.inc("likeCount",1);
        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(true);
        Comment andModify = mongoTemplate.findAndModify(query, update, options, Comment.class);

    }
    //小视频的评论点赞
    public void addCommentsDisLike(Comment comment,Object commentId) {
        Query query = Query.query(Criteria.where("id").is(commentId));
        Update update = new Update();
        update.inc("likeCount",-1);
        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(true);
        Comment andModify = mongoTemplate.findAndModify(query, update, options, Comment.class);

    }

}
